package com.e3.sysmg.config.server;

import java.util.ArrayList;
import java.util.List;

import com.e3.core.base.ServiceException;
import com.e3.sysmg.config.client.ParameterHelper;
import com.e3.sysmg.config.client.ParameterRPCService;
import com.e3.sysmg.config.model.ParameterModel;
import com.e3.sysmg.config.model.ParameterQC;
import com.e3.sysmg.config.model.ParameterTreeModel;
import com.e3.web.core.client.WebException;
import com.e3.web.core.server.GwtRPCAction;
import com.e3.web.core.server.GwtRPCExecutor;
import com.e3.web.core.server.GwtRPCVoidExecutor;

public class ParameterRPCServiceImpl extends GwtRPCAction implements
		ParameterRPCService {

	public List<ParameterTreeModel> getParameterTypes(ParameterTreeModel model)	throws WebException {
		if(model == null){
			return ParameterHelper.INSTANCE.getParameterTreeRootModel();
		}
		else if(model.getCode()==0){
			return ParameterHelper.INSTANCE.getParameterTreeData();
		}
		return null;
	}

	public ParameterModel newParameter(final ParameterModel model) throws WebException {
		return new GwtRPCExecutor<ParameterModel>() {
			@Override
			public ParameterModel execute() throws ServiceException, Exception {
				return ParameterServiceClient.getParameterService().newParameter(model);
			}
		}.doExecute();
	}

	public ParameterModel modifyParameter(final ParameterModel model) throws WebException {
		return new GwtRPCExecutor<ParameterModel>() {
			@Override
			public ParameterModel execute() throws ServiceException, Exception {
				return ParameterServiceClient.getParameterService().modifyParameter(model);
			}
		}.doExecute();
	}

	public List<ParameterModel> getParameterByCondition(final ParameterQC goodsQC) throws WebException {
		return new GwtRPCExecutor<List<ParameterModel>>() {
			@Override
			public List<ParameterModel> execute() throws ServiceException, Exception {
				List<ParameterModel> models = ParameterServiceClient.getParameterQueryService().getParameterByCondition(goodsQC);
				return models;
			}
		}.doExecute();
	}

	public void deleteParameters(List<ParameterModel> models) throws WebException {
		if(models == null||models.isEmpty()){
			return;
		}
		final List<Long> ids = new ArrayList<Long>(models.size());
		for (ParameterModel goodsModel : models) {
			ids.add(goodsModel.getId());
		}
		
		new GwtRPCVoidExecutor() {
			@Override
			public void execute() throws ServiceException, Exception {
				ParameterServiceClient.getParameterService().deleteParameterByIds(ids);
			}
		}.doExecute();
	}
}
